/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.delaphone.flocash.web.action;

import com.delaphone.flocash.service.AccountService;
import com.delaphone.flocash.web.service.MailService;
import com.delaphone.flocash.service.SystemService;
import com.delaphone.flocash.service.UserService;
import com.delaphone.flocash.web.util.Validate;
import com.opensymphony.xwork2.Action;
import java.util.ArrayList;
import org.jpos.gl.Currency;

/**
 *
 * @author tamdx
 */
public class ReceiveCashAction extends BaseAction {

    private UserService userService;
    private MailService mailService;
    private SystemService sysService;
    private String email;
    private String mobile;
    private String amount;
    private String currency;
    private String currencyName;
    private String note;
    private ArrayList<Currency> currencies;

    public String step1() {
        if (user == null) {
            return Action.LOGIN;
        }

        return Action.SUCCESS;
    }

    public String step2() {
        if (user == null) {
            return Action.LOGIN;
        }
        validateFeild();
        if (getFieldErrors().size() > 0) {
            addActionError("Please check some input");
            request.setAttribute("email", getEmail());
            request.setAttribute("mobile", getMobile());
            request.setAttribute("amount", getAmount());
            return Action.INPUT;
        }
        return Action.SUCCESS;
    }

    public String step3() {
        if (user == null) {
            return Action.LOGIN;
        }
        userService.saveReceiveMoneyReq(user.getId(), getEmail(), getMobile(), getAmount(), getCurrency(), getNote());
        mailService.sendMail(getEmail(), "FloCash - Payment request",
                getText("user.receivecash.email_template",
                new String[]{getEmail(), user.getEmail(), getAmount(), getCurrencyName(), getNote()}));
        return Action.SUCCESS;
    }

    public MailService getMailService() {
        return mailService;
    }

    public void setMailService(MailService mailService) {
        this.mailService = mailService;
    }

    public SystemService getSysService() {
        return sysService;
    }

    public void setSysService(SystemService sysService) {
        this.sysService = sysService;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

    public String getCurrency() {
        return currency;
    }

    public void setCurrency(String currency) {
        this.currency = currency;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public ArrayList<Currency> getCurrencies() {
        currencies = (ArrayList<Currency>) sysService.getAllCurrencies();
        return currencies;
    }

    public void setCurrencies(ArrayList<Currency> currencies) {
        this.currencies = currencies;
    }

    public String getCurrencyName() {
        currencyName = sysService.getCurrencyById(Integer.valueOf(getCurrency())).getSymbol();
        return currencyName;
    }

    public void setCurrencyName(String currencyName) {
        this.currencyName = currencyName;
    }

    public void validateFeild() {
        if (Validate.isEmptyString(getEmail()) || !Validate.isEmail(getEmail())) {
            addFieldError("email", "*");
        } else if (!Validate.isEmptyString(getMobile()) && !Validate.isPhoneNumber(getMobile())) {
            addFieldError("mobile", "*");
        } else if (Validate.isEmptyString(getAmount()) || !Validate.isNumber(getAmount())) {
            addFieldError("amount", "*");
        }
    }
}
